package com.jaydenxiao.common.baseapp;

import android.os.Handler;
import android.os.Message;

import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.base.BaseFragment;

import java.lang.ref.SoftReference;

/**
 * Created by hp on 2017/11/22.
 */
public class LeakSafeHandler2<K extends BaseFragment> extends Handler {
    private SoftReference<K> fragment;
    public LeakSafeHandler2(K fragment){
        this.fragment = new SoftReference(fragment);
    }
    @Override
    public void handleMessage(Message msg) {
        K fragment = getFragment();
        if(fragment == null){
            return;
        }
        if(msg.what == BaseActivity.WHAT_DELAY_BTN){
            fragment.setBtnClickable(true);
        }else{
            onMessageReceived(fragment,msg);
        }
    }
    public K getFragment(){
        if(fragment != null){
            return fragment.get();
        }
        return null;
    }
    public void onMessageReceived(K fragment,Message msg){}
}
